package com.itdianzi.controller.factory;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.controller.util.FileUploadUtil;
import com.itdianzi.domain.cargo.Factory;
import com.itdianzi.domain.cargo.FactoryExample;
import com.itdianzi.domain.cargo.Product;
import com.itdianzi.domain.cargo.ProductExample;
import com.itdianzi.service.cargo.FactoryService;
import com.itdianzi.service.cargo.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Controller
@RequestMapping("/cargo/factoryProduct")
public class FactoryProductController extends BaseController {

    @Reference
    private ProductService productService;

    @Reference
    private FactoryService factoryService;

    /**
     * 跳转到产品页面
     *
     * @param page
     * @param size
     * @param factoryId
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, String factoryId) {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria productExampleCriteria = productExample.createCriteria();
        productExampleCriteria.andFactoryIdEqualTo(factoryId);
        PageInfo pageInfo = productService.selectByExample(page, size, productExample);
        request.setAttribute("page", pageInfo);
        return "cargo/factoryProduct/product-list";
    }

    /**
     * 跳转到查看页面
     *
     * @param id
     * @return
     */
    @RequestMapping("/toView")
    public String toView(String id) {
        //根据产品ID查询对应产品
        Product product = productService.findById(id);
        request.setAttribute("product", product);
        return "cargo/factoryProduct/product-view";
    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        //查询出所有厂家的信息
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeBetween("货物", "附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        return "cargo/factoryProduct/product-add";
    }


    @Autowired
    private FileUploadUtil fileUploadUtil;

    /**
     * 进行添加或修改操作
     *
     * @param product
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Product product,String factoryId, MultipartFile productPhoto) throws Exception {
        //判断
        if (StringUtils.isBlank(product.getId())) {
            if (productPhoto != null) { //进行文件上传  别人已经选了图片对象
                String imagePath = fileUploadUtil.upload(productPhoto);
                //添加成功后返回一个互联网图片的路径
                product.setProductImage(imagePath);
            }
            productService.save(product);
        } else {
            productService.update(product);
        }
        return "redirect:/cargo/factoryProduct/list.do?factoryId="+factoryId;
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //查询出所有厂家的信息
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeBetween("货物", "附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        Product product = productService.findById(id);
        request.setAttribute("product", product);
        return "cargo/factoryProduct/product-update";
    }


    /**
     * 根据ID进行删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(String id,String factoryId) {
        productService.deleteById(id);
        return "redirect:/cargo/factoryProduct/list.do?factoryId="+factoryId;
    }
}
